package cn.skyclass.utils
{
	/**
	 * 用于时间数值操作的工具类
	 */
	public class DateUtil
	{
		/**
		 * start with Sunday at index 0
		 */
		public static const WEEK_NAME_ARRAY:Array = 
			["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
		
		/**
		 * start with Sunday at index 0
		 */
		public static const WEEK_NAME_ARRAY_CHAR:Array = 
			["日", "一", "二", "三", "四", "五", "六"];
		
		/**
		 * start with Sunday at index 0
		 */
		public static const WEEK_NAME_ARRAY_EN:Array = 
			["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
		
		/**
		 * total milliseconds of a day
		 */
		public static const MS_PER_DAY:Number = 24 * 60 * 60 * 1000;
		
		/**
		 * total milliseconds of an hour
		 */		
		public static const MS_PER_HOUR:Number = 60 * 60 * 1000;
		

		/**
		 * 在某个日期上加上指定的天数，如天数是负数则是减去指定的天数
		 */
		public static function addDay(date:Date, dayCount:int):Date{
			var msDelta:Number = dayCount * MS_PER_DAY;
			return new Date(date.time + msDelta);
		}
		
		/**
		 * 在某个日期上加上指定的小时数，如时间数是负数则是减去相应的小时数
		 */
		public static function addHour(date:Date, hourCount:int):Date{
			var msDelta:Number = hourCount * MS_PER_HOUR;
			return new Date(date.time + msDelta);
		}		
		
		/**
		 * 获取日期的星期信息，返回示例：星期二
		 */
		public static function getWeekName(date:Date):String{
			return WEEK_NAME_ARRAY[date.day];
		}
		
				
		/**
		 * 判断两个日期是否是在同一星期里
		 * @param date1
		 * @param date2
		 * @param sundayIsFirstDay
		 * @return 
		 */
		public static function inSameWeek(date1:Date, date2:Date, 
			sundayIsFirstDay:Boolean=false):Boolean{
				
			if(!date1 || !date2){
				return false;
			}
			
			var deltaTime:Number = date1.time - date2.time;
			var deltaDay:int = getDeltaDay(date1, date2);
			
			if(deltaTime < 0){
				// if data1 < date2
				return deltaDay < getModifiedDay(date2);
			}else if(deltaTime > 0){
				// if data1 > date2
				return deltaDay < getModifiedDay(date1);
			}else{
				return true;
			}
			
			function getModifiedDay(d:Date):Number{
				if(sundayIsFirstDay){
					// make 1 for Sunday, 2 for Monday, and so on
					return d.day + 1;
				}else{
					// make 7 for Sunday, 1 for Monday, and so on
					return d.day == 0 ? 7 : d.day;
				}
			}
		}
		
		/**
		 * 获取两个日期间相隔的天数。
		 * 若是同一天，则返回0；任何一个日期为空值，则返回0；示例：
		 * getDeltaDay(new Date(2008, 4, 9), new Date(2008, 4, 11)) = 2 
		 * getDeltaDay(new Date(2008, 4, 9, 23, 59, 59), new Date(2008, 4, 10, 0, 0, 0)) = 1 
		 * getDeltaDay(new Date(2008, 4, 9, 23, 59, 59), new Date(2008, 4, 9, 0, 0, 0)) = 0 
		 * @param date1
		 * @param date2
		 * @return 
		 */		
		public static function getDeltaDay(date1:Date, date2:Date):int{
			if(!date1 || !date2){
				return 0;
			}
			
			// just use the year, month, and date properties
			date1 = new Date(date1.fullYear, date1.month, date1.date);			
			date2 = new Date(date2.fullYear, date2.month, date2.date);			
			
			var deltaTime:Number = date1.time - date2.time;
			var deltaDay:Number = Math.abs(deltaTime) / DateUtil.MS_PER_DAY;
			return int(deltaDay);
		}
		
		/**
		 * 获取两个日期间相隔的天数, date1 - date2。
		 * 若是同一天，则返回0；否则返回正数或负数
		 * @param date1
		 * @param date2
		 * @return 
		 */				
		public static function dateDiff(date1:Date, date2:Date):int {
			if(!date1 || !date2){
				return 0;
			}
			
			// just use the year, month, and date properties
			date1 = new Date(date1.fullYear, date1.month, date1.date);			
			date2 = new Date(date2.fullYear, date2.month, date2.date);			
			
			var deltaTime:Number = date1.time - date2.time;
			var deltaDay:Number = deltaTime / DateUtil.MS_PER_DAY;
			return int(deltaDay);
		}
		
		/**
		 * 格式化时间长度信息。例：
		 * formatTimeLength(93860, true) --> 1天2小时4分钟20秒
		 * formatTimeLength(93860, false) --> 1天2小时4分钟
		 */
		public static function formatTimeLength(totalSeconds:Number, showSeconds:Boolean = true):String{
			totalSeconds = isNaN(totalSeconds) ? 0 : Math.floor(totalSeconds);
			
			var seconds:Number = totalSeconds % 60;
			var minutes:Number = Math.floor(totalSeconds / 60);
			var hours:Number = Math.floor(minutes / 60);
			var days:Number = Math.floor(hours / 24);
			minutes %= 60;
			hours %= 24;
			
			var out:String = "";
			if(days){
				out += days + "天";
			}
			if(hours){
				out += hours + "小时";
			}
			if(minutes){
				out += minutes + "分钟";
			}
			if(seconds && showSeconds){
				out += seconds + "秒";
			}
			return out;
		}
		
				
	}
}